﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using Business.IFace;
using MB.Orm.DB;
using Public.DataAccess;
using MB.RuleBase.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using WoChu.Autorun.Base.Model;
using System.Data;

namespace Business
{
    public class BOrderPick
    {
        //获取周转箱任务信息，根据周转箱号
        public List<WCS_TO_WMS> GetWCS_TO_WMSlistByBoxNo(string BoxNo)
        {
            try
            {
                return DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<WCS_TO_WMS>("OrderPick", "GetWCS_TO_WMSlistByBoxNo", BoxNo);
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                if (ex.Message.Contains("ORA-03135"))
                {
                    return DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<WCS_TO_WMS>("OrderPick", "GetWCS_TO_WMSlistByBoxNo", BoxNo);
                }
                return null;
            }
        }
        //获取周转箱任务信息，根据订单号 
        public List<WCS_TO_WMS> GetWCS_TO_WMSlistByOrderNo(string OrderNo)
        {
            try
            {
                return DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<WCS_TO_WMS>("OrderPick", "GetWCS_TO_WMSlistByOrderNo", OrderNo);
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                if (ex.Message.Contains("ORA-03135"))
                {
                    return DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<WCS_TO_WMS>("OrderPick", "GetWCS_TO_WMSlistByOrderNo", OrderNo);
                }
                return null;
            }
        }

        //根据周转箱号获取发货任务信息
        public List<WMS_TO_WCS_TASK> GetWMS_TO_WCS_TASKlistByZBoxNo(string BoxNo)
        {
            try
            {
                return DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<WMS_TO_WCS_TASK>("OrderPick", "GetWMS_TO_WCS_TASKlistByZBoxNo", BoxNo);
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                if (ex.Message.Contains("ORA-03135"))
                {
                    return DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<WMS_TO_WCS_TASK>("OrderPick", "GetWMS_TO_WCS_TASKlistByZBoxNo", BoxNo);
                }
                return null;
            }
        }

        //获取销售订单明细
        public List<SaleOrderItem1> GetSaleOrderItem1listByOrderNo(string OrderNo)
        {
            try
            {
                return DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<SaleOrderItem1>("OrderPick", "GetSaleOrderItemBysheetid", OrderNo);
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                if (ex.Message.Contains("ORA-03135"))
                {
                    return DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<SaleOrderItem1>("OrderPick", "GetSaleOrderItemBysheetid", OrderNo);
                }
                return null;
            }
        }

        //获取APP订单明细
        public List<SaleAppItem1> GetSaleAppItem1listByOrderNo(string OrderNo)
        {
            try
            {
                return DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<SaleAppItem1>("OrderPick", "GetSaleAppItemBysheetid", OrderNo);
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                if (ex.Message.Contains("ORA-03135"))
                {
                    return DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<SaleAppItem1>("OrderPick", "GetSaleAppItemBysheetid", OrderNo);
                }
                return null;
            }
        }

        //获取周转箱任务信息，根据订单号 
        public DataSet GetSaleOrderItemlistByOrderNo(string OrderNo)
        {
            try
            {
                return DatabaseExcuteByXmlHelper.NewInstance.GetDataSetByXml("OrderPick", "GetSaleOrderItemByOrderNo", OrderNo);
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                if (ex.Message.Contains("ORA-03135"))
                {
                    return DatabaseExcuteByXmlHelper.NewInstance.GetDataSetByXml("OrderPick", "GetSaleOrderItemByOrderNo", OrderNo);
                }
                return null;
            }
        }

        //获取订单信息，根据泡沫箱号
        public List<OrderInfo> GetOrderInfoByFBoxNo(string FBoxNo)
        {
            try
            {
                return DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<OrderInfo>("OrderPick", "GetOrderInfoByFBoxNo", FBoxNo);
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                if (ex.Message.Contains("ORA-03135"))
                {
                    return DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<OrderInfo>("OrderPick", "GetOrderInfoByFBoxNo", FBoxNo);
                }
                return null;
            }
        }

        //获取订单信息，根据票号
        public List<OrderInfo> GetOrderInfoByTicketNO(string TicketNO)
        {
            try
            {
                return DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<OrderInfo>("OrderPick", "GetOrderInfoByTicketNO", TicketNO);
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                if (ex.Message.Contains("ORA-03135"))
                {
                    return DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<OrderInfo>("OrderPick", "GetOrderInfoByTicketNO", TicketNO);
                }
                return null;
            }
        }
        public ResultModel GetsUerInfo(string userCode, string psw)
        {
            try
            {
                AppUserList userInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<AppUserList>("NewWebCommon", "GetUserInfo", userCode).FirstOrDefault();
                if (userInfo != null)
                {
                    string password = userInfo.PASSWORD;
                    password = Crypto.DeCrypt(password);
                    if (psw == password)
                    {
                        return new ResultModel { RESULT = "success", MESSAGE = "登录成功", DATA = userInfo };
                    }
                }
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                if (ex.Message.Contains("ORA-03135"))
                {
                    AppUserList userInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<AppUserList>("NewWebCommon", "GetUserInfo", userCode).FirstOrDefault();
                    if (userInfo != null)
                    {
                        string password = userInfo.PASSWORD;
                        password = Crypto.DeCrypt(password);
                        if (psw == password)
                        {
                            return new ResultModel { RESULT = "success", MESSAGE = "登录成功", DATA = userInfo };
                        }
                    }
                }
                return new ResultModel { RESULT = "failed", MESSAGE = "登录异常：" + ex.Message, DATA = null };
            }
            return new ResultModel { RESULT = "failed", MESSAGE = "用户信息为空", DATA = null };
        }

    }
}
